package com.wxy.path_files;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.SQLOutput;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 47868
 */
public class files操作 {
    public static void func1() throws IOException {     //筛选文件
        Files.walkFileTree(Paths.get("D://代码//Netty"), new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { //筛选
                if (file.toString().endsWith(".java")) {
                    System.out.println(file);
                }
                return super.visitFile(file,attrs);
            }
        });
    }

    public static void func2() throws IOException {     //读取和遍历文件以及文件夹
        AtomicInteger dirCount = new AtomicInteger(0);
        AtomicInteger fileCount = new AtomicInteger(0);
        Files.walkFileTree(Paths.get("D://代码//Netty"), new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {  //遍历输出文件夹
                System.out.println("====>" + dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { //遍历输出文件
                System.out.println(file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        System.out.println(dirCount);
        System.out.println(fileCount);
        System.out.println("-------------------------------");
    }

    public static void func3(String path) throws IOException {          //删除目录中的文件
        Files.walkFileTree(Paths.get(path),new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    public static void func4(String sourcePath,String targetPath) throws IOException {      //文件拷贝
        Files.walk(Paths.get(sourcePath)).forEach(path->{
            try {
                String targetFileName = path.toString().replaceFirst(sourcePath,targetPath);
                if(Files.isDirectory(path)){
                    Files.createDirectory(Paths.get(targetFileName));
                } else if(Files.isRegularFile(path)) {
                    Files.copy(path,Paths.get(targetFileName));
                }
            }
            catch (IOException e){
                System.out.println(e.getMessage());
            }
        });
    }
    public static void main(String[] args) throws IOException {
//        func1();
//        func2();
        //创建删除目录起始条件
//        Files.createDirectories(Paths.get("wxy//files"));
//        Files.createFile(Paths.get("wxy//files//wxy.txt"));
//        FileChannel channel = new RandomAccessFile("wxy//files//wxy.txt", "rw").getChannel();
//        ByteBuffer buffer = ByteBuffer.allocate(10);
//        buffer.put("1234567890".getBytes());
//        buffer.flip();
//        channel.write(buffer);
        //func3("D://代码//Netty//wxy");
        func4("wxy","zyy");
    }
}
